package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.IAttendanceDataRecordDAO;
import org.jsola.hr.dto.AttendanceDataRecordAddDTO;
import org.jsola.hr.dto.AttendanceDataRecordUpdateDTO;
import org.jsola.hr.entity.AttendanceDataRecordDO;
import org.jsola.hr.provider.ISalaryTemplateProviderService;
import org.jsola.hr.query.AttendanceDataRecordQuery;
import org.jsola.hr.query.AttendanceDataTempQuery;
import org.jsola.hr.service.IAttendanceDataRecordService;
import org.jsola.hr.service.IAttendanceDataTempService;
import org.jsola.hr.vo.AttendanceDataRecordListVO;
import org.jsola.hr.vo.AttendanceDataRecordVO;
import org.jsola.salary.entity.SalaryTemplateSalaryDO;
import org.jsola.salary.service.ISalaryTemplateSalaryService;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 考勤导入记录表
 *
 * @author zhr
 */
@Service("hrAttendanceDataRecordServiceImpl")
public class AttendanceDataRecordServiceImpl implements IAttendanceDataRecordService {

    @Autowired
    private IAttendanceDataRecordDAO attendanceDataRecordDAO;
    @Autowired
    private IAttendanceDataTempService attendanceDataTempService;
    @Autowired
    private ISalaryTemplateSalaryService salaryTemplateSalaryService;

    @Override
    public Page<AttendanceDataRecordListVO> pageList(AttendanceDataRecordQuery attendanceDataRecordQuery, TokenUser tokenUser) {
        attendanceDataRecordQuery.setIsImport(1);
        Page<AttendanceDataRecordListVO> attendanceDataRecordListVOPage = selectPage(attendanceDataRecordQuery,tokenUser.getSiteId());
        for (AttendanceDataRecordListVO
                entity : attendanceDataRecordListVOPage.getEntities()) {
            AttendanceDataTempQuery attendanceDataTempQuery = new AttendanceDataTempQuery();
            attendanceDataTempQuery.setRecordId(entity.getId());
            attendanceDataTempQuery.setIsError(0);
            entity.setEmpCount(attendanceDataTempService.selectCount(attendanceDataTempQuery,tokenUser.getSiteId()));
            if(StringUtils.isEmpty(entity.getSalaryTemplateSalaryId())){
                entity.setIsHandle(false);
            }else {
                SalaryTemplateSalaryDO salaryTemplateSalaryDO = salaryTemplateSalaryService.selectById(Long.parseLong(entity.getSalaryTemplateSalaryId()), tokenUser);
                if(salaryTemplateSalaryDO != null && salaryTemplateSalaryDO.getIsArchive() != null){
                    entity.setIsHandle(salaryTemplateSalaryDO.getIsArchive());
                }else {
                    entity.setIsHandle(false);
                }
            }
        }
        return attendanceDataRecordListVOPage;
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public AttendanceDataRecordVO save(AttendanceDataRecordAddDTO attendanceDataRecordAddDTO, TokenUser tokenUser) {
        // 转DO
        AttendanceDataRecordDO attendanceDataRecordDO = attendanceDataRecordAddDTO.to(AttendanceDataRecordDO.class);
        // 保存
        attendanceDataRecordDO = save(attendanceDataRecordDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return attendanceDataRecordDO.to(AttendanceDataRecordVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(AttendanceDataRecordUpdateDTO attendanceDataRecordUpdateDTO, TokenUser tokenUser) {
        //转DO
        AttendanceDataRecordDO attendanceDataRecordDO = attendanceDataRecordUpdateDTO.to(AttendanceDataRecordDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(attendanceDataRecordDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... attendanceDataRecordIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) attendanceDataRecordIds);
    }


    @Override
    public AttendanceDataRecordVO selectById(String attendanceDataRecordId, String siteId) {
        AttendanceDataRecordDO attendanceDataRecordDO = selectDOById(attendanceDataRecordId, siteId);
        if (attendanceDataRecordDO == null) {
            return null;
        }
        return attendanceDataRecordDO.to(AttendanceDataRecordVO.class);
    }

    @Override
    public List<AttendanceDataRecordListVO> select(AttendanceDataRecordQuery attendanceDataRecordQuery, String siteId) {
        List<AttendanceDataRecordDO> attendanceDataRecordDOList = selectDO(attendanceDataRecordQuery, siteId);
        if(CollectionUtils.isEmpty(attendanceDataRecordDOList)) {
            return attendanceDataRecordDOList == null ? null : new ArrayList<>();
        }
        return attendanceDataRecordDOList.stream()
                .map(attendanceDataRecordDO -> attendanceDataRecordDO.to(AttendanceDataRecordListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(AttendanceDataRecordQuery attendanceDataRecordQuery, String siteId) {
        Example example = buildExample(attendanceDataRecordQuery,siteId);
        return attendanceDataRecordDAO.selectCountByExample(example);
    }

    @Override
    public Page<AttendanceDataRecordListVO> selectPage(AttendanceDataRecordQuery attendanceDataRecordQuery, String siteId) {
        Example example = buildExample(attendanceDataRecordQuery,siteId);
        Page<AttendanceDataRecordDO> page = attendanceDataRecordDAO.selectPageByExample(example,
                attendanceDataRecordQuery.getPageNo(),
                attendanceDataRecordQuery.getPageSize());

        return page.to(AttendanceDataRecordListVO.class);
    }

    @Override
    public AttendanceDataRecordDO selectDOById(String attendanceDataRecordId, String siteId) {
        return listById(attendanceDataRecordId, siteId);
    }

    @Override
    public List<AttendanceDataRecordDO> selectDO(AttendanceDataRecordQuery attendanceDataRecordQuery, String siteId) {
        Example example = buildExample(attendanceDataRecordQuery,siteId);
        return attendanceDataRecordDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example

     * @param attendanceDataRecordQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(AttendanceDataRecordQuery attendanceDataRecordQuery, String siteId) {
        Example example = new Example(AttendanceDataRecordDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        // 排序
        if(!StringUtils.isEmpty(attendanceDataRecordQuery.getYear())){
            example.and().andEqualTo("year",attendanceDataRecordQuery.getYear());
        }
        if(!StringUtils.isEmpty(attendanceDataRecordQuery.getMonth())){
            example.and().andEqualTo("month",attendanceDataRecordQuery.getMonth());
        }
        if(!StringUtils.isEmpty(attendanceDataRecordQuery.getAttendanceExcelName())){
            example.and().andLike("fileName","%"+attendanceDataRecordQuery.getAttendanceExcelName()+"%");
        }
        if(!StringUtils.isEmpty(attendanceDataRecordQuery.getCompanyId())){
            example.and().andEqualTo("companyId",attendanceDataRecordQuery.getCompanyId());
        }
        if(attendanceDataRecordQuery.getIsImport() != null){
            if(attendanceDataRecordQuery.getIsImport()==1){
                example.and().andEqualTo("isImport",true);
            }else if(attendanceDataRecordQuery.getIsImport()==0){
                example.and().andEqualTo("isImport",false);
            }
        }
        ExampleKit.setExampleOrder(example,attendanceDataRecordQuery.getOrders());
        return example;
    }
}




